using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using AspNetForums.Components;

namespace AspNetForums.Admin
{
	/// <summary>
	/// Summary description for UserRolesAdmin.
	/// </summary>
	public class UserRolesAdmin : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.ListBox listAvailableRoles;
		protected System.Web.UI.WebControls.Button btnAddRole;
		protected System.Web.UI.WebControls.Button btnRemoveRole;
		protected System.Web.UI.WebControls.ListBox listAssignedRoles;
		protected System.Web.UI.WebControls.Label lblUserName;
		protected System.Web.UI.WebControls.Button btnSave;
		protected User user;
		private void Page_Load(object sender, System.EventArgs e)
		{
			user = Users.GetUser(ForumContext.Current.UserID, false );

			#region Old Code
//			foreach( Role r in userRoles ) 
//			{
//				for( int n = availableRoles.Count - 1; n > -1 ; n-- ) {
//					Role s = (Role)availableRoles[n];
//
//					if( s.CompareTo( r ) == 0 ) 
//					{
//						availableRoles.RemoveAt( n );
//						break;
//					}
//
//				}
//
//				if( r.RoleID == 0 )
//					userRoles.Remove( r );
//				foreach( Role s in availableRoles ) {
//					if( s.CompareTo( r ) == 0 )
//						availableRoles.Re
//				}
//				if( availableRoles.Contains( r )) {
//					int n = availableRoles.IndexOf( r );
//
//					availableRoles.RemoveAt( n );
//				}
//			}
			#endregion

			lblUserName.Text = user.Username;

			btnSave.Click +=new EventHandler(btnSave_Click);
			btnAddRole.Click +=new EventHandler(btnAddRole_Click);
			btnRemoveRole.Click +=new EventHandler(btnRemoveRole_Click);

			if( !Page.IsPostBack ) 
			{
				ArrayList userRoles			= Roles.GetRoles( user.UserID );
				ArrayList availableRoles	= Roles.GetRoles();

				for( int m = userRoles.Count - 1; m >= 0; m-- )
				{
					if( ((Role)userRoles[m]).RoleID == 0 )
						userRoles.RemoveAt(m);
				}

				for( int n = availableRoles.Count - 1; n >= 0; n-- )
				{
					if( ((Role)availableRoles[n]).RoleID == 0 )
						availableRoles.RemoveAt( n );
				}

				for( int j = 0; j < userRoles.Count; j++ )
				{
					if( availableRoles.Contains( ((Role)userRoles[j]).RoleID.ToString() ) )
					{
						availableRoles.Remove( ((Role)userRoles[j]).RoleID.ToString() );
					}
				}

				listAvailableRoles.DataSource		= availableRoles;
				listAvailableRoles.DataTextField	= "Name";
				listAvailableRoles.DataValueField	= "RoleID";

				listAssignedRoles.DataSource		= userRoles;
				listAssignedRoles.DataTextField		= "Name";
				listAssignedRoles.DataValueField	= "RoleID";

				DataBind();
			}

		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void btnSave_Click(object sender, EventArgs e)
		{
			ArrayList existingRoles = Roles.GetRoles( user.UserID, false );

			// remove any duplicate roles in the assigned roles that are already present in the existing roles
			foreach( Role r in existingRoles )
			{
				if( listAssignedRoles.Items.Contains( new ListItem( r.Name, r.RoleID.ToString()) ))
				{
					listAssignedRoles.Items.Remove( r.RoleID.ToString() );
				}
				else
				{
					// the role has been removed from the list, so remove it from the db
					Roles.RemoveUserFromRole( user.UserID, r.RoleID );
				}
			}

			foreach( ListItem item in listAssignedRoles.Items )
			{
				Roles.AddUserToRole( user.UserID, Int32.Parse( item.Value ) );
			}
		}

		private void btnAddRole_Click(object sender, EventArgs e)
		{
			System.Diagnostics.Debug.Assert( listAvailableRoles.DataSource != null );

			SwapListItem( listAvailableRoles, listAssignedRoles );
			listAvailableRoles.SelectedIndex = -1;
			listAssignedRoles.SelectedIndex	= -1;
		}

		private void btnRemoveRole_Click(object sender, EventArgs e)
		{
			SwapListItem( listAssignedRoles, listAvailableRoles );
			listAvailableRoles.SelectedIndex = -1;
			listAssignedRoles.SelectedIndex	= -1;
		}

		protected void SwapListItem( ListBox source, ListBox destination )
		{
			ListItem item = source.SelectedItem;

			if( item != null )
			{
				destination.Items.Add( item );
				source.Items.Remove( item );
			}
		}
	}
}
